import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

public class MainScreen {
	public static File dataDirectory = new File(System.getProperty("user.home") + "\\stockdata");
	
	public static void main(String[] args) {
		if(!dataDirectory.exists()) {
				dataDirectory.mkdir();
		}
		
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setBounds(40, 40, 730, 600);
		shell.setText("SDIP - Stock Data and Investment Performance.");
		StockData stockdata_var = new StockData();
		ArrayList<Fstock> stocklist = stockdata_var.getAll();
		
		final Table stocktable = new Table(shell,SWT.FULL_SELECTION|SWT.MULTI);
		stocktable.setBounds(30,30,650,200);
		stocktable.setHeaderVisible(true);
		
		TableColumn stocksymbol_col = new TableColumn(stocktable,SWT.LEFT);
		stocksymbol_col.setText("Symbol");
		stocksymbol_col.setWidth(70);
		
		TableColumn stockname_col = new TableColumn(stocktable,SWT.LEFT);
		stockname_col.setText("Name");
		stockname_col.setWidth(180);
		
		TableColumn stocksharecount_col = new TableColumn(stocktable,SWT.LEFT);
		stocksharecount_col.setText("Share Count");
		stocksharecount_col.setWidth(90);
		
		TableColumn stocklastprice_col = new TableColumn(stocktable,SWT.LEFT);
		stocklastprice_col.setText("Last Price");
		stocklastprice_col.setWidth(70);
		
		TableColumn stockoriginal_col = new TableColumn(stocktable,SWT.LEFT);
		stocklastprice_col.setText("Original Price");
		stocklastprice_col.setWidth(100);
		
		TableColumn stockdaysgain_col = new TableColumn(stocktable,SWT.LEFT);
		stockdaysgain_col.setText("Days Gain");
		stockdaysgain_col.setWidth(70);
		
		TableColumn stocktotalgain_col = new TableColumn(stocktable,SWT.LEFT);
		stocktotalgain_col.setText("Total Gain");
		stocktotalgain_col.setWidth(70);
		
		TableColumn stockexchange_col = new TableColumn(stocktable,SWT.LEFT);
		stockexchange_col.setText("Exchange");
		stockexchange_col.setWidth(70);
		Color red = new Color(display, 255,22,0);
		Color green = new Color(display, 51,241,0);
		
		// GROUP - 1 GET CORREL
		final Group group = new Group(shell,SWT.BORDER);
		group.setText("Get Correlation");
		group.setBounds(30, 250, 500, 60);
		final Label l1 = new Label(group,SWT.NONE);
		l1.setText("Stock-1");
		final Text stock_text1 = new Text(group, SWT.BORDER);
		final Text stock_text2 = new Text(group, SWT.BORDER);
		final Label l2 = new Label(group,SWT.NONE);
		l2.setText("Stock-2");
		l1.setLocation(20, 23);
		stock_text1.setLocation(60, 20);
		l2.setLocation(160, 23);
		stock_text2.setLocation(200, 20);
		l1.pack();
		l2.pack();
		stock_text1.pack();
		stock_text2.pack();
		Button getCorel = new Button(group, SWT.PUSH);
		getCorel.setText("Get Corel");
		getCorel.setLocation(300, 18);
		getCorel.pack();
		final Label l_result = new Label(group,SWT.NONE);
		l_result.setText("Result");
		l_result.setLocation(370, 23);
		l_result.pack();
		
		// GROUP - 2 BUY SELL
		final Group group2 = new Group(shell,SWT.BORDER);
		group2.setText("Execute OffLine Transaction");
		group2.setBounds(30, 330, 500, 60);
		final Label l_head = new Label(group2,SWT.NONE);
		l_head.setText("Please Enter Data");
		l_head.setLocation(20, 23);
		final Text stock_sym = new Text(group2, SWT.BORDER);
		stock_sym.setLocation(120,20);
		final Text stock_quant = new Text(group2, SWT.BORDER);
		stock_quant.setLocation(190,20);
		final Text stock_price = new Text(group2, SWT.BORDER);
		stock_price.setLocation(260,20);
		Button Buy = new Button(group2, SWT.PUSH);
		Buy.setText("  Buy  ");
		Buy.setLocation(320,18);
		Button Sell= new Button(group2, SWT.PUSH);
		Sell.setText("  Sell  ");
		Sell.setLocation(370,18);
		Buy.pack();
		Sell.pack();
		stock_price.setText("Price...");
		stock_sym.setText("Ticker..");
		stock_quant.setText("Quantity");
		l_head.pack();
		stock_sym.pack();
		stock_quant.pack();
		stock_price.pack();
		
		// HISTORICAL DATA
		// GROUP - 1 GET CORREL
		final Group group3 = new Group(shell,SWT.BORDER);
		group3.setText("Get Historical Data");
		group3.setBounds(30, 410, 500, 60);
		final Label l3 = new Label(group3,SWT.NONE);
		l3.setText("Ticker Symbol");
		l3.setLocation(15,23);
		final Text stock_text3 = new Text(group3, SWT.BORDER);
		stock_text3.setLocation(100, 20);
		l3.pack();
		stock_text3.pack();
		Button getHistoricalData = new Button(group3, SWT.PUSH);
		getHistoricalData.setText("Download Historical Data");
		getHistoricalData.setLocation(200, 20);
		getHistoricalData.pack();
		final Label l_result2 = new Label(group3,SWT.NONE);
		l_result2.setText("");
		l_result2.setLocation(15, 40);
		l_result2.pack();
		
		// HISTORICAL DATA
		// GROUP - 1 GET CORREL
		final Group group4 = new Group(shell,SWT.BORDER);
		group4.setText("Get Sharpe Ratio");
		group4.setBounds(30, 490, 500, 60);
		final Label l4 = new Label(group4,SWT.NONE);
		l4.setText("Ticker Symbol");
		l4.setLocation(15,23);
		final Text stock_text4 = new Text(group4, SWT.BORDER);
		stock_text4.setLocation(100, 20);
		l4.pack();
		stock_text4.pack();
		Button getSharpeRatio = new Button(group4, SWT.PUSH);
		getSharpeRatio.setText("Get Sharpe Ratio");
		getSharpeRatio.setLocation(200, 20);
		getSharpeRatio.pack();
		final Label l_result3 = new Label(group4,SWT.NONE);
		l_result3.setText("");
		l_result3.setLocation(15, 40);
		l_result3.pack();
		
		
		for(int loop1=0;loop1<stocklist.size();loop1++)
		{
			Fstock ftemp = stocklist.get(loop1);
			TableItem stockrow = new TableItem(stocktable,SWT.NONE);
			
			
			stockrow.setText(0, ftemp.getSymbol());
			stockrow.setText(1, ftemp.getFullname());
			stockrow.setText(2, Double.toString(ftemp.getStockcount()));
			stockrow.setText(3, Float.toString(ftemp.getLastprice()));
			stockrow.setText(4, Float.toString(ftemp.getOriginalprice()));
			stockrow.setText(5, Float.toString(ftemp.getDaysgain()));
			stockrow.setText(6, Float.toString(ftemp.getTotalgain()));
			stockrow.setText(7, ftemp.getExchange());
			if(ftemp.getTotalgain()<0)
			{
				stockrow.setBackground(red);
			}
			else
			{
				stockrow.setBackground(green);
			}
			
		}
		
//		Button getCorel = new Button(shell,SWT.PUSH);
//		getCorel.setText("Get CoRel");
//		getCorel.setLocation(40,250);
		
		Button getcorel_allstrats = new Button(shell,SWT.PUSH);
		getcorel_allstrats.setText("Get All possible correls");
		getcorel_allstrats.setLocation(40,280);
		
		
		
		final Label result = new Label(shell,SWT.NONE);
	    result.setText("Status");
	    result.setLocation(150, 250);
	    
	    
	    
	    // All my functions here
	    
//	    getcorel_allstrats.addSelectionListener(new SelectionAdapter() 
//		{
//			public void widgetSelected(SelectionEvent e)
//			{
//				if(stocktable.getSelectionCount() == 2)
//				{
//					int stock1 = stocktable.getSelectionIndices()[0];
//					int stock2 = stocktable.getSelectionIndices()[1];
//					String stockname1 = stocktable.getItem(stock1).getText(0);
//					String stockname2 = stocktable.getItem(stock2).getText(0);
//					//String out = "Stock1 = " + stockname1.toString() + " and Stock2 = " + stockname2.toString();
//					CorelData coreldata_var = new CorelData();
//					try{
//					Double corel_value = coreldata_var.getCorel(stockname1, stockname2);
//					String out = "Stocks selected are : " + stockname1 + " & " + stockname2; 
//					 out += "\nThe correl = " + corel_value.toString();
//					result.setText(out);
//					result.pack();
//					System.out.println(out);
//					}
//					catch (Exception ex)
//					{
//						ex.printStackTrace();
//					}
//				}
//			}
//		});
	    
	    getCorel.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e)
			{
				// Put the code here to compute all possible pairs
				// sort the result and put it in a scrollable table.
				CorelData coreldata_var = new CorelData();
				try{
				Double corel_value = coreldata_var.getCorel(stock_text1.getText(), stock_text2.getText());
				//String out = "Stocks selected are : " + stockname1 + " & " + stockname2; 
				 //out += "\nThe correl = " + corel_value.toString();
				//result.setText(out);
				//result.pack();
				if (corel_value.toString().length()>=5) {
					l_result.setText( corel_value.toString().substring(0, 5));
				}
				else {
					l_result.setText(corel_value.toString());
				}
								
				l_result.pack();
				
				//System.out.println(out);
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
				
			}
		});
	    
	    getHistoricalData.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e)
			{
				// Put the code here to compute all possible pairs
				// sort the result and put it in a scrollable table.
				CorelData coreldata_var = new CorelData();
				try {
				coreldata_var.getHistoricalData(stock_text3.getText());
				l_result2.setText("Data downloaded to: " + dataDirectory.getCanonicalPath()+ "\\" + stock_text3.getText() + ".csv");
				l_result2.pack();
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
	    
	    getSharpeRatio.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e)
			{
				// Put the code here to compute all possible pairs
				// sort the result and put it in a scrollable table.
				CorelData coreldata_var = new CorelData();
				try {
				Double sharpe = coreldata_var.getSharpeRatio(stock_text4.getText());
				if (sharpe.toString().length()>=5) {
					l_result3.setText(sharpe.toString().substring(0, 5));
				}
				else {
					l_result3.setText(sharpe.toString());
				}
				l_result3.pack();
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		// All the packaging
		result.pack();
		getcorel_allstrats.pack();
		getCorel.pack();
		shell.open();
		
		 while (!shell.isDisposed()) 
		 {
		      if (!display.readAndDispatch())
		        display.sleep();
		 }
		    display.dispose();
		 
		
		
	}
}